package com.lzy.cacheAnno;

import com.lzy.conf.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 *
 */
@Component
@Aspect
@Slf4j
public class RedisCacheDelAspect {
    RedisUtil redisUtil;

    @Autowired
    public RedisCacheDelAspect(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }

    @Pointcut("@annotation(com.lzy.cacheAnno.RedisCacheDel)")
    private void RedisCacheDel() {
    }

    @After("RedisCacheDel()")
    public void RedisCacheDel(JoinPoint pjp) {
        try {
            Object[] args = pjp.getArgs();//得到方法执行所需的参数
            MethodSignature signature = (MethodSignature) pjp.getSignature();
            RedisCacheDel annotation = AnnotationUtils.getAnnotation(signature.getMethod(), RedisCacheDel.class);
            String[] value = annotation.value();
            String key = annotation.key();
            String extKey = annotation.extKey();
            boolean isFor = annotation.isFor();
            String separate = annotation.separate();

            ExpressionParser ep = new SpelExpressionParser();
            EvaluationContext ctx = new StandardEvaluationContext();
            for (int i = 0; i < value.length; i++) {
                ctx.setVariable(value[i], args[i]);
            }
            if(isFor){
                if (!extKey.equals("")) {
                    extKey = ep.parseExpression(extKey).getValue(ctx).toString();
                    for (String s : extKey.split(separate)) {
                        String redis_key = key + ":" + s;
                        Collection<String> keys = redisUtil.keys(redis_key);
                        for (String itemKey : keys) {
                            redisUtil.deleteObject(itemKey);
                        }
                    }
                }
            }else{
                String redis_key = key;
                if (!extKey.equals("")) {
                    extKey = ep.parseExpression(extKey).getValue(ctx).toString();
                    redis_key = key + ":" + extKey;
                }
                Collection<String> keys = redisUtil.keys(redis_key);
                for (String itemKey : keys) {
                    redisUtil.deleteObject(itemKey);
                }
            }

        } catch (Throwable e) {
            log.error("缓存删除注解切面异常", e);
            throw new RuntimeException(e);
        } finally {
        }
    }

}
